package api;

import cn.hutool.json.JSONUtil;
import comment.ApplicationVariable;
import model.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginout")
public class LoginOutServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType(ApplicationVariable.APPLICATION_JSON);
        int state = -1;
        HttpSession session = req.getSession(false);
        if(session != null && session.getAttribute(ApplicationVariable.SESSION_USER_KET) != null){
            //登录状态，需要退出
            //移除session
            session.removeAttribute(ApplicationVariable.SESSION_USER_KET);
            state = 200;
        }
        resp.getWriter().println(JSONUtil.toJsonStr(new ResultData(state,"","")));
    }
}
